<MudTreeView ServerData="LoadServerData" Items="_treeItems" Hover="true" Width="100%">
    <ItemTemplate Context="item">
        <MudTreeViewItem Value="@item.Value" CanExpand="true" Icon="@Icons.Material.Filled.Label">
            <BodyContent>
                <div class="mud-treeview-item-bodycontent" style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%;">
                    <MudText Style="justify-self: start;">@item.Text</MudText>
                    <div style="justify-self: end;">
                        <MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Medium" Color="Color.Inherit" />
                        <MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" Color="Color.Inherit" />
                    </div>
                </div>
            </BodyContent>
        </MudTreeViewItem>
    </ItemTemplate>
</MudTreeView>

@code {
    private IReadOnlyCollection<TreeItemData<string>> _treeItems = [];
    public bool SimulateUpdateRoot = false;
    public bool SimulateUpdate3 = false;

    protected override async Task OnInitializedAsync()
    {
        _treeItems = await LoadServerData(null);

        await base.OnInitializedAsync();
    }

    public Task<IReadOnlyCollection<TreeItemData<string>>> LoadServerData(string? parentValue)
    {
        if (parentValue is null)
        {
            if (!SimulateUpdateRoot)
            {

                return Task.FromResult<IReadOnlyCollection<TreeItemData<string>>>(
                [
                    new TreeItemData("This is item one"),
                    new TreeItemData("This is item two"),
                    new TreeItemData("This is item three"),
                    new TreeItemData("This is item six")
                ]);
            }

            return Task.FromResult<IReadOnlyCollection<TreeItemData<string>>>(
            [
                new TreeItemData("This is item one"),
                new TreeItemData("This is item 2"),
                new TreeItemData("This is item three"),
                new TreeItemData("This is item six")
            ]);
        }
        if (parentValue == "This is item three")
        {
            if (!SimulateUpdate3)
            {
                return Task.FromResult<IReadOnlyCollection<TreeItemData<string>>>(
                [
                    new TreeItemData("This is item four"),
                    new TreeItemData("This is item five")
                ]);
            }

            return Task.FromResult<IReadOnlyCollection<TreeItemData<string>>>(
            [
                new TreeItemData("This is item 4"),
                new TreeItemData("This is item 5")
            ]);
        }

        return Task.FromResult<IReadOnlyCollection<TreeItemData<string>>>([]);
    }

    public sealed class TreeItemData : TreeItemData<string>
    {
        public TreeItemData(string value) : base(value)
        {
            Text = value;
        }
    }
}
